import React, { Component } from 'react'
import axios from 'axios'
import './App.css'
import Search from './Search'
import List from './List'

export default class App extends Component {

  state = {
    users: [],
    isFirst: true,
    isLoading: false,
    err: ''
  }

  searchItem = (keyWord) => {
    this.setState({isFirst: false, isLoading: true})
    // 模板语言  后端配置了cors
    axios.get(`https://api.github.com/search/users?q=${keyWord}`).then(
      response => {
        console.log('成功了', response.data)
        this.setState({isLoading: false, users: response.data.items, err: ''})
      },
      error => {
        console.log('失败了', error)
        this.setState({isLoading: false, err: error.message})
    }
    )
  }
  render() {
    return (
      <div>
        <Search searchItem={this.searchItem}/>
        <List {...this.state}/>
      </div>
    )
  }
}
